package com.txt.call;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.BoolRes;
import android.support.annotation.IdRes;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.library.login.AccessKandy;
import com.example.library.control.TxtKandy;

public class MainActivity extends Activity {
    private final String TAG=MainActivity.class.getSimpleName();
    private Button mDoCall;
    private Button  mDoMpvCall;
    private TextView mPhone;
    private RadioGroup mRdiaoGroup;
    private RadioButton mVideoButton;
    private RadioButton mAudioButton;
    private boolean isVideo=true;
    private boolean isLogin=false;
    private Button mLogoutButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView() {
        mRdiaoGroup= (RadioGroup) findViewById(R.id.redioGoup);
        mVideoButton= (RadioButton) findViewById(R.id.video);
        mAudioButton= (RadioButton) findViewById(R.id.audio);
        mRdiaoGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (mVideoButton.getId()==checkedId){
                    isVideo=true;
                    Log.d(TAG, "onCheckedChanged: 1");
                }else if (mAudioButton.getId()==checkedId){
                    isVideo=false;
                    Log.d(TAG, "onCheckedChanged: 2");
                }
            }
        });
        mDoCall= (Button) findViewById(R.id.docall);
        mPhone= (TextView) findViewById(R.id.phone);
        mPhone.setText(TxtKandy.getAccessKandy().getLoginUser());
        mDoCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TxtKandy.getKandyCall().showDoCallDialog(MainActivity.this,isVideo);
            }
        });
        mDoMpvCall= (Button) findViewById(R.id.dompvcall);
        mDoMpvCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TxtKandy.getConnnectCall().skipDoCallMpv(MainActivity.this);
            }
        });
        mLogoutButton= (Button) findViewById(R.id.logout);
        mLogoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TxtKandy.getAccessKandy().logout(new AccessKandy.LogoutRequestCallBack() {
                    @Override
                    public void onSuccess() {
                        MainActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this,"退出登录！",Toast.LENGTH_SHORT).show();
                                Intent intent=new Intent(MainActivity.this,LoginActivity.class);
                                startActivity(intent);
                                TxtKandy.getDataMpvConnnect().clearData();
                                MainActivity.this.finish();
                            }
                        });
                    }
                    @Override
                    public void onFail(int i, String s) {
                        Log.d(TAG, "onFail: logout errcode"+i+"errmsg"+s);
                        MainActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this,"退出失败！",Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                });
            }
        });
    }

    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }
}
